package com.code2roc.fastboot.config;

import com.code2roc.fastboot.Intecept.PageRedirectInterceptor;
import com.code2roc.fastboot.condition.DefaultWebConfigCondition;
import com.code2roc.fastboot.file.MinioFileOperate;
import com.code2roc.fastboot.file.MinioFileStoargeProperty;
import com.code2roc.fastboot.framework.config.WebConfig;
import com.code2roc.fastboot.framework.file.IFileOperate;
import com.code2roc.fastboot.framework.file.LocalFileOperate;
import com.code2roc.fastboot.framework.file.LocalFileStoargeProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.servlet.MultipartConfigElement;

@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy
@EnableAsync
@Conditional({DefaultWebConfigCondition.class})
public class BootWebConfig extends WebConfig {
    @Autowired
    private PageRedirectInterceptor pageRedirectInterceptor;
    @Autowired
    private Environment env;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pageRedirectInterceptor).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Value("${system.maxFileSize}")
    private int maxFileSize;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(maxFileSize + "MB");
        factory.setMaxRequestSize(maxFileSize * 10 + "MB");
        return factory.createMultipartConfig();
    }

    //minio文件存储实现
    @Bean(name="minio")
    public IFileOperate minioFileOperate() {
        String endpoint = env.getProperty("file.minio.endpoint");
        String accessKey = env.getProperty("file.minio.accessKey");
        String secrectKey = env.getProperty("file.minio.secrectKey");
        MinioFileStoargeProperty minioFileStoargeProperty = new MinioFileStoargeProperty(endpoint,accessKey,secrectKey);
        return new MinioFileOperate(minioFileStoargeProperty);
    }
}
